/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package NetworkReceiver;
import NetworkPackage.*;
import FrameWork.Item;

/**
 * this class is used to take a message in xml format from the networkobserver and 
 * converts it to item and decrypts it if it was encrypted and the add it in the queue of the job scheduler  
 */
public class MessageObserver {
static MessageObserver instance;
boolean state;              // this flag is indicating if the class is in use currently or not because we are uing threads
SecurityHandler decryptor;  // this instace of the ecurityhandler class is used to decrypt the mesages
XMLHandler translator;      // this intance of XMLHandler is used to convert the incoming mesage from xml format to our item class

    MessageObserver() {
        decryptor = new SecurityHandler(); 
        translator = XMLHandler.getInstance();
    }
   public static MessageObserver getInstance()
   {
       if(instance == null)
       {
           instance = new MessageObserver();
       }
       return instance;
   }
   public boolean isBusy()      // get the state of the class currently
   {
       return state;
   }
   public Item translate(String msg)    // used to convert the message from xml to item
   {
       return translator.buildItemFromXML(msg);
   }
   public Item decryptItem(Item item)   // used to decrypt the incoming message 
   {
       if(item.getEncryptionEnabled()){
           if(item.getEncrypted())
           {
               item.setData(decryptor.encrypt_decrypt(item.getData(),false));
               item.setEncrypted(false);
           }
       }
       return item; 
   }
   public void addItem(Item item)       // used to add the item after preparing it in the jobscheduler queue  
   {
       JobScheduler jobScheduler = JobScheduler.getInstance();
       jobScheduler.addItem(item);
   }
}
